import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_app/BaseMaterialWidget.dart';

/// ClipOval:圆形剪裁
/// ClipRRect:圆角矩形剪裁
/// ClipRect:矩形剪裁
/// ClipPath:路径剪裁
class ClipDemo extends BaseMaterialWidget {
  @override
  Widget buildContent(BuildContext context) {
    return ListView(
      children: <Widget>[
        _createClipOval(),
        _createClipRRect(),
        _createClipRect(),
        _createClipPath()
      ],
    );
  }

  @override
  String getTitle() {
    return "剪裁处理";
  }

  ///路径剪裁
  Widget _createClipPath() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 20),
      child: ClipPath(
        clipper: TriangleCliper(),
        child: SizedBox(
          width: 150,
          height: 150,
          child: Image.asset(
            'images/lake.jpg',
            fit: BoxFit.fill,
          ),
        ),
      ),
    );
  }


  ///矩形
  Widget _createClipRect() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 20),
      child: ClipRect(
        child: SizedBox(
          width: 150,
          height: 150,
          child: Image.asset('images/lake.jpg', fit: BoxFit.fill),
        ),
      ),
    );
  }

  ///圆角矩形
  Widget _createClipRRect() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 20),
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(30)),
        child: SizedBox(
          width: 150,
          height: 150,
          child: Image.asset(
            "images/lake.jpg",
            fit: BoxFit.fill,
          ),
        ),
      ),
    );
  }

  //圆形
  Widget _createClipOval() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 20),
      child: ClipOval(
        child: SizedBox(
          width: 150,
          height: 150,
          child: Image.asset(
            'images/lake.jpg',
            fit: BoxFit.fill,
          ),
        ),
      ),
    );
  }
}

class TriangleCliper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path()
    ..moveTo(50, 50)
    ..lineTo(50, 10)
    ..lineTo(100, 50)
    ..close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return true;
  }

}
